package com.wrench.elevator.tida.controller;

import java.io.IOException;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSON;
import com.wrench.elevator.tida.constant.Result;
import com.wrench.elevator.tida.domain.wuye.Wuye_User;
import com.wrench.elevator.tida.exception.WrenchException;
import com.wrench.elevator.tida.service.LoginService;
import com.wrench.elevator.tida.util.SendMessage;
import com.wrench.elevator.tida.util.WrenchUtil;

@Controller
public class LoginController
{
    private static final Logger logger = LoggerFactory.getLogger(LoginController.class);
    
    @Resource
    private LoginService loginService;


    @RequestMapping(value = "/verifyCode", method = RequestMethod.POST)
    public void verifyCode(HttpServletRequest request, HttpServletResponse response, ModelMap map){
        logger.debug("==>**********enter in verifyCode(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
        String tel=WrenchUtil.getPara(request,"tel");
        String code= null;
        try {
            code=SendMessage.send(tel);
        } catch (IOException e) {
            e.printStackTrace();
        }
        map.put(Result.RESULT_CODE,Result.SUCCESS_CODE);
        map.put(Result.RESULT_MESSAGE,Result.RESULT_MESSAGE);
        map.put("code",code);
        logger.debug("==> response = {}", JSON.toJSON(map));
        WrenchUtil.responseMap(response, map);
        map = null;
        logger.debug("==>**********exit out verifyCode(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");


    }

    @RequestMapping(value = "/resetPwd", method = RequestMethod.POST)
    public void resetPwd(HttpServletRequest request, HttpServletResponse response, ModelMap map){
        logger.debug("==>**********enter in resetPwd(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
            String tel=WrenchUtil.getPara(request,"phone");
            String pwd=WrenchUtil.getPara(request,"password");
            WrenchException error=null;
        try {
            loginService.resetPwd(tel,pwd);
        } catch (WrenchException e) {
          error=e;
        }
        if(WrenchUtil.hasNull(error)){
            map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
            map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
            map.put("message", "resetPwd success");
        }else{
            map.put(Result.RESULT_CODE, error.getResultCode());
            map.put(Result.RESULT_MESSAGE,error.getResultMessage());
        }
        logger.debug("==> response = {}", JSON.toJSON(map));
        WrenchUtil.responseMap(response, map);
        map = null;

        logger.debug("==>**********enter in resetPwd(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");

    }


    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public void login(HttpServletRequest request, HttpServletResponse response, ModelMap map)
    {
        logger.debug("==>**********enter in login(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
        String user_tel = WrenchUtil.getPara(request, "user_tel");
        String user_pwd = WrenchUtil.getPara(request, "user_pwd");
        
        logger.debug("==> params : user_tel = {} , user_pwd = {} ", user_tel, user_pwd);
        
        Wuye_User user = null;
        WrenchException error = null;
        try
        {
            user = loginService.login(user_tel, user_pwd);
        }
        catch (WrenchException e)
        {
            error = e;
        }
        
        if (WrenchUtil.hasNull(error))
        {
            map.clear();
            if (WrenchUtil.hasNull(user))
            {
                map.put(Result.RESULT_CODE, Result.LOGIN_FAIL_CODE);
                map.put(Result.RESULT_MESSAGE, Result.LOGIN_FAIL_MESSAGE);
            }
            else
            {
                map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
                map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
                map.put("message", "login success");
                map.put("user", user);
                
            }
            
        }
        else
        {
            map.clear();
            map.put(Result.RESULT_CODE, error.getResultCode());
            map.put(Result.RESULT_MESSAGE, error.getResultMessage());
        }
        logger.debug("==> response = {}", JSON.toJSON(map));
        WrenchUtil.responseMap(response, map);
        map = null;
        
        logger.debug("==>**********exit out login(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
    }



}
